package com.nand.module.function.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.nand.module.function.R;
import com.nand.module.function.bean.PollingBean;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.view.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

public class PatrolPageAdapter extends RecyclerView.Adapter<PatrolPageAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private int type;

    private List<PollingBean> mList = new ArrayList<>();

    private OnItemClickListener mOnItemClickListener = null;
    private String cont = "";
    private CommonPopupWindow window;
    private TextView horiText;

    public PatrolPageAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<PollingBean> data, int type) {
        this.mList = data;
        this.type = type;
        notifyDataSetChanged();
    }

    public List<PollingBean> getPollingBean() {
        return mList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_patrol_page_details, viewGroup, false);
        layout.setOnClickListener(this);
        return new ViewHolder(layout);
    }

    private int OrQu = 1;

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        String accContent = mList.get(position).getAccContent();
        final String standContent = mList.get(position).getStandContent();
        viewHolder.tvJcx.setText(accContent);
        viewHolder.tvJcnr.setText(standContent);
        viewHolder.radioGraoup.clearCheck();
        switch (type) {
            case 1: //详情
                int result = mList.get(position).getResult();

                String reasonNG = mList.get(position).getReasonNG();
                if (reasonNG.isEmpty() || reasonNG == null){
                    viewHolder.textView.setVisibility(View.GONE);
                }else {
                    viewHolder.ll_bhgyy.setVisibility(View.VISIBLE);
                    viewHolder.view_line.setVisibility(View.VISIBLE);
                    viewHolder.editText.setVisibility(View.GONE);
                    viewHolder.textView.setVisibility(View.VISIBLE);
                    viewHolder.textView.setText(reasonNG);
                }
                for (int i = 0; i < viewHolder.radioGraoup.getChildCount(); i++) {
                    if (0 == result){
                        LogUtils.e("aaaaaa==="+result);
                        viewHolder.radioButton2.setChecked(true);
                    }else {
                        viewHolder.radioButton1.setChecked(true);
                        LogUtils.e("aaaaaa==="+result);
                    }
                   // viewHolder.radioGraoup.getChildAt(i).setBackgroundResource(R.drawable.ctr_btn);
                    viewHolder.radioGraoup.getChildAt(i).setEnabled(false);
                }
                break;
            case 2:  //新增
                viewHolder.radioButton1.setChecked(false);
                viewHolder.ll_bhgyy.setVisibility(View.GONE);
                viewHolder.radioGraoup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (checkedId == R.id.radio_button1) {
                            if (viewHolder.radioButton1.isChecked()) {
                                viewHolder.ll_bhgyy.setVisibility(View.GONE);
                                viewHolder.view_line.setVisibility(View.GONE);
                                OrQu = 1;
                                mList.get(position).setResult(OrQu);
                                mList.get(position).setReasonNG("");
                            }
                        } else if (checkedId == R.id.radio_button2) {
                            if (viewHolder.radioButton2.isChecked()) {
                                viewHolder.ll_bhgyy.setVisibility(View.VISIBLE);
                                viewHolder.view_line.setVisibility(View.VISIBLE);
                                //     Log.e(TAG, "你最爱的职业是: " + radioButton2.getText().toString());
                                OrQu = 0;
                                mList.get(position).setReasonNG("");
                                mList.get(position).setResult(OrQu);
                            }
                        }
                    }
                });

                viewHolder.editText.setText("");
                viewHolder.editText.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before,
                                              int count) {
                        cont = s.toString();
                        mList.get(position).setReasonNG(cont);
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                break;
        }

        viewHolder.tvJcnr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onJcnrClick(position,standContent,viewHolder.tvJcnr);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RadioGroup radioGraoup;
        RadioButton radioButton1;
        RadioButton radioButton2;
        EditText editText;
        TextView tvJcx, tvJcnr,textView;
        LinearLayout ll_bhgyy;
        View view_line;

        public ViewHolder(View itemView) {
            super(itemView);
            radioGraoup = itemView.findViewById(R.id.radio_group);
            radioButton1 = itemView.findViewById(R.id.radio_button1);
            radioButton2 = itemView.findViewById(R.id.radio_button2);
            editText = itemView.findViewById(R.id.edittext);
            tvJcx = itemView.findViewById(R.id.tv_jcx);
            tvJcnr = itemView.findViewById(R.id.tv_jcnr);
            textView = itemView.findViewById(R.id.textview);
            ll_bhgyy = itemView.findViewById(R.id.ll_bhgyy);
            view_line = itemView.findViewById(R.id.view_line);

        }

    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onJcnrClick(int position, String context, TextView a);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}


